﻿using System.Net;
using System.Text;

namespace CoHubServerCore.Tcp;

public class TcpClient(System.Net.Sockets.TcpClient instance)
{
    public Guid Guid { get; } = Guid.NewGuid();
    public EndPoint RemoteEndPoint => instance.Client.RemoteEndPoint!;

    public string? Receive(int maxLength = 4_096)
    {
        var buffer = new byte[maxLength];
        var received = instance.GetStream().Read(buffer);
        return received == 0 ? null : Encoding.UTF8.GetString(buffer, 0, received);
    }

    public void Send(string message)
    {
        var stream = instance.GetStream();
        stream.Write(Encoding.UTF8.GetBytes(message));
        stream.Flush();
    }

    public void Close() => instance.Close();
}